package qq2564874169.tuaotuao.fx.orm.mybatis;


import qq2564874169.tuaotuao.fx.orm.CursorReader;
import qq2564874169.tuaotuao.fx.orm.MultipleReader;
import qq2564874169.tuaotuao.fx.orm.SqlParam;
import qq2564874169.tuaotuao.fx.orm.query.PageData;
import qq2564874169.tuaotuao.fx.orm.query.PageSet;
import qq2564874169.tuaotuao.fx.orm.query.SqlQuery;

import java.util.List;

public interface MybatisQuery extends SqlQuery {
    <T> List<T> stmtQuery(Class<T> clazz, SqlParam param);

    MultipleReader stmtMultiple(SqlParam param);

    <T> PageData<T> stmtPager(Class<T> clazz, PageSet pageSet, SqlParam param);

    <T> CursorReader<T> stmtCursor(Class<T> clazz, SqlParam param, int fetchSize, boolean independent);

    default <T> PageData<T> stmtPager(PageSet pageSet, SqlParam param) {
        return stmtPager(null, pageSet, param);
    }

    default <T> CursorReader<T> stmtCursor(SqlParam param, int fetchSize, boolean independent) {
        return stmtCursor((Class<T>) null, param, fetchSize, independent);
    }

    default <T> CursorReader<T> stmtCursor(String statement, Object param, int fetchSize, boolean independent) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtCursor(sp, fetchSize, independent);
    }

    default <T> CursorReader<T> stmtCursor(String statement, Object param, boolean independent) {
        return stmtCursor(statement, param, 1, independent);
    }

    default <T> CursorReader<T> stmtCursor(String statement, Object param) {
        return stmtCursor(statement, param, false);
    }

    default <T> CursorReader<T> stmtCursor(String statement, boolean independent) {
        return stmtCursor(statement, null, independent);
    }

    default <T> CursorReader<T> stmtCursor(String statement) {
        return stmtCursor(statement, false);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement, Object param, int fetchSize, boolean independent) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtCursor(clazz, sp, fetchSize, independent);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement, Object param, boolean independent) {
        return stmtCursor(clazz, statement, param, 1, independent);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement, Object param, int fetchSize) {
        return stmtCursor(clazz, statement, param, fetchSize, false);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement, Object param) {
        return stmtCursor(clazz, statement, param, false);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement, boolean independent) {
        return stmtCursor(clazz, statement, null, independent);
    }

    default <T> CursorReader<T> stmtCursor(Class<T> clazz, String statement) {
        return stmtCursor(clazz, statement, false);
    }

    default <T> PageData<T> stmtPager(PageSet pageSet, String statement) {
        return stmtPager(pageSet, statement, null);
    }

    default <T> PageData<T> stmtPager(PageSet pageSet, String statement, Object param) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtPager(pageSet, sp);
    }

    default <T> PageData<T> stmtPager(Class<T> clazz, PageSet pageSet, String statement) {
        return stmtPager(clazz, pageSet, statement, null);
    }

    default <T> PageData<T> stmtPager(Class<T> clazz, PageSet pageSet, String statement, Object param) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtPager(clazz, pageSet, sp);
    }

    default MultipleReader stmtMultiple(String statement, Object param) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtMultiple(sp);
    }

    default MultipleReader stmtMultiple(String statement) {
        return stmtMultiple(statement, null);
    }

    default <T> List<T> stmtQuery(String statement, Object param) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtQuery((Class<T>) null, sp);
    }

    default List stmtQuery(String statement) {
        return stmtQuery(statement, null);
    }

    default <T> List<T> stmtQuery(Class<T> clazz, String statement, Object param) {
        SqlParam sp = SqlParam.createFor(statement, param, false);
        return stmtQuery(clazz, sp);
    }

    default <T> List<T> stmtQuery(Class<T> clazz, String statement) {
        return stmtQuery(clazz, statement, null);
    }

    default Object stmtQueryOne(String statement) {
        return stmtQueryOne(null, statement, null);
    }

    default <T> T stmtQueryOne(Class<T> clazz, String statement) {
        return stmtQueryOne(clazz, statement, null);
    }

    default <T> T stmtQueryOne(Class<T> clazz, String statement, Object param) {
        List<T> list = stmtQuery(clazz, statement, param);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
}
